package com.imooc.diveinspringboot.mybatis.service.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author Chris
 * @version 1.0.0
 * @date 2019/05/14
 * @since 1.0.0
 */
public class MyInvocationHandler implements InvocationHandler {
    private Object target;

    public MyInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("target 执行之前....");
        return method.invoke(target, args);
    }

    public static void main(String[] args) {
        MyInvocationHandler invocationHandler = new MyInvocationHandler(new DemoImpl());
        Demo proxy = (Demo) Proxy.newProxyInstance(MyInvocationHandler.class.getClassLoader(), new Class[]{Demo.class}, invocationHandler);
        System.out.println("proxy = " + proxy.getClass().getName());
        proxy.test1();
        // proxy.test2();
    }
}
